package controller.commands;

import java.util.ArrayList;
import model.graphic.GraphicModel;
import model.graphic.locations.Location;
import model.graphic.objects.GConnection;
import model.graphic.objects.GHandleConnection;
import model.graphic.objects.GInput;
import model.graphic.objects.GInputPort;
import model.graphic.objects.GNode;
import model.graphic.objects.GOutput;
import model.graphic.objects.GOutputPort;
import model.graphic.objects.Graphical;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Ben
 */
public class OneOnOneTranslationCommand extends BasicCommand{
    private GraphicView gview;
    private GraphicModel gmodel;

    public OneOnOneTranslationCommand(GraphicView gview, GraphicModel gmodel){
        this.gview = gview;
        this.gmodel = gmodel;
    }

    @Override
    public void execute() {
        ArrayList<Graphical> allNodes = gmodel.getAllNodes();
        //only use the nodes with one connected outport to an inport
        for(Graphical node : allNodes){
            ArrayList<GConnection> connections = ((GNode)node).getConns();
            if(connections.size() == 1 && connections.get(0).getClass().isAssignableFrom(GHandleConnection.class)){
                GInputPort inPutPort = ((GHandleConnection)connections.get(0)).getInput();
                GOutputPort outputPort = ((GOutputPort)((GHandleConnection)connections.get(0)).getOutput().getBase());
                GNode outNode = (GNode)outputPort.getBase();
                //move outNode to inNode
                Location target = inPutPort.getUpperLeft();
                Location loc = outNode.getGraphLocation();
                int outWidth = outNode.getLowerRight().getX() - outNode.getLowerLeft().getX();//GStoreNodes must have width 0
                int x = target.getX() - outWidth - outputPort.getWidth()- GOutput.ARROW_DELTA - GInput.EXTRA_WIDTH;
                loc.setXY(x, target.getY());
            }
        }
        gview.repaint();
    }

}
